<template>
  <div class="component-distribution-user">
    <!-- 搜索栏 -->
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item :label="$t('distributionMsg.nickName') + ':'">
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item :label="$t('distribUserWallet.mobileTelNumber') + ':'">
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('distribUserWallet.mobileTelNumber')"
            />
          </el-form-item>
          <el-form-item
            prop="banReason"
            :label="$t('home.reasons') + ':'"
          >
            <el-select
              v-model="searchForm.banReason"
              :placeholder="$t('home.reasons')"
              clearable
            >
              <el-option
                :label="$t('distribUserWallet.lostContact')"
                :value="0"
              />
              <el-option
                :label="$t('distribUserWallet.maliciousSwipe')"
                :value="1"
              />
              <el-option
                :label="$t('distribUserWallet.other')"
                :value="2"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            prop="state"
            :label="$t('publics.status') + ':'"
          >
            <el-select
              v-model="searchForm.state"
              :placeholder="$t('publics.status')"
              clearable
            >
              <el-option
                :label="$t('publics.normal')"
                :value="1"
              />
              <el-option
                :label="$t('distribUserWallet.ban')"
                :value="2"
              />
              <el-option
                :label="$t('distribUserWallet.perBan')"
                :value="-1"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetSearch('searchForm')"
            >
              {{ $t('product.reset') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>

    <div class="main-container">
      <!-- 表格 -->
      <div class="table-con distribution-ban-table">
        <el-table
          ref="distributionBanTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
        >
          <!-- 分销员昵称 -->
          <el-table-column
            prop="nickName"
            :label="$t('distribUserWallet.distributorNickname')"
          >
            <template #default="scope">
              <span class="table-cell-text line-clamp-one">{{ scope.row.distributionUser.nickName }}</span>
            </template>
          </el-table-column>
          <!-- 分销员手机号 -->
          <el-table-column
            prop="userMobile"
            :label="$t('distribUserWallet.mobileTelNumber')"
          >
            <template #default="scope">
              {{ scope.row.distributionUser.userMobile }}
            </template>
          </el-table-column>
          <!-- 原因 -->
          <el-table-column
            prop="banReason"
            :label="$t('home.reasons')"
          >
            <template #default="scope">
              <div
                v-if="!scope.row.banReason && scope.row.banReason !== 0"
                class="tag-text"
              >
                -
              </div>
              <div
                v-if="scope.row.banReason === 0"
                class="tag-text"
              >
                {{ $t("distribUserWallet.lostContact") }}
              </div>
              <div
                v-if="scope.row.banReason === 1"
                class="tag-text"
              >
                {{ $t("distribUserWallet.maliciousSwipe") }}
              </div>
              <div
                v-if="scope.row.banReason === 2"
                class="tag-text"
              >
                {{ $t("distribUserWallet.other") }}
              </div>
            </template>
          </el-table-column>
          <!-- 备注 -->
          <el-table-column
            prop="remarks"
            :label="$t('publics.remark')"
          >
            <template #default="scope">
              <div v-if="!scope.row.remarks">
                无
              </div>
              <div
                v-else
                class="table-cell-text"
              >
                {{ scope.row.remarks }}
              </div>
            </template>
          </el-table-column>
          <!-- 操作人 -->
          <el-table-column
            prop="username"
            :label="$t('distribUserWallet.operator')"
          >
            <template #default="scope">
              <span v-if="scope.row.sysUser">{{ scope.row.sysUser.username }}</span>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <!-- 操作时间 -->
          <el-table-column
            prop="updateTime"
            :label="$t('distribUserWallet.operatorTime')"
            sortable
          >
            <template #default="scope">
              <span v-if="!scope.row.updateTime">无</span>
              <span v-else>{{ scope.row.updateTime }}</span>
            </template>
          </el-table-column>
          <!-- 状态 -->
          <el-table-column
            align="center"
            prop="state"
            :label="$t('publics.status')"
          >
            <template #default="scope">
              <div
                v-if="scope.row.state === -1"
                class="tag-text"
              >
                {{ $t("distribUserWallet.perBan") }}
              </div>
              <div
                v-if="scope.row.state === 1"
                class="tag-text"
              >
                {{ $t("publics.normal") }}
              </div>
              <div
                v-if="scope.row.state === 2"
                class="tag-text"
              >
                {{ $t("distribUserWallet.ban") }}
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <el-pagination
        v-if="dataList.length"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="onPageSizeChange"
        @current-change="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
onMounted(() => {
  getDataList()
})

/**
 * 点击查询
 * @param newData
 */
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}

let tempSearchForm = null // 保存上次点击查询的请求条件
const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = ref({})
const dataList = ref([])
const dataListLoading = ref(false)
/**
 * 获取数据列表
 * @param pageParam
 * @param newData
 */
const getDataList = (pageParam, newData = false) => {
  dataListLoading.value = true
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm.value))
  }
  http({
    url: http.adornUrl('/distribution/distributionUserBan/page/anduser'),
    method: 'get',
    params: http.adornParams(Object.assign({
      current: pageParam ? pageParam.currentPage : page.currentPage,
      size: pageParam ? pageParam.pageSize : page.pageSize
    }, tempSearchForm))
  })
    .then(({ data }) => {
      page.total = data.total
      page.pageSize = data.size
      page.currentPage = data.current
      dataList.value = data.records
      dataListLoading.value = false
    })
}
defineExpose({ getDataList })

const searchFormRef = ref(null)
/**
 * 重置表单
 */
const onResetSearch = () => {
  searchFormRef.value?.resetFields()
  searchForm.value = {}
}

const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}
</script>
